package com.shuda.business.crm.controller;

import com.shuda.business.crm.dto.CustomerDTO;
import com.shuda.business.crm.dto.CustomerQueryDTO;
import com.shuda.business.crm.service.CustomerService;
import com.shuda.common.core.domain.PageDTO;
import com.shuda.common.core.domain.PageResult;
import com.shuda.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CRM客户管理控制器
 */
@RestController
@RequestMapping("/api/crm/customers")
@Tag(name = "CRM客户管理", description = "CRM客户管理相关接口")
@RequiredArgsConstructor
public class CustomerController {

    private final CustomerService customerService;

    /**
     * 获取客户分页列表
     */
    @GetMapping("/page")
    @Operation(summary = "获取客户分页列表", description = "根据查询条件获取客户分页列表")
    public R<PageResult<CustomerDTO>> getCustomerPage(
            @Parameter(description = "分页参数") PageDTO pageDTO,
            @Parameter(description = "查询条件") CustomerQueryDTO queryDTO) {
        return R.ok(customerService.getCustomerPage(pageDTO, queryDTO));
    }

    /**
     * 获取客户详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取客户详情", description = "根据ID获取客户详情")
    public R<CustomerDTO> getCustomerById(@PathVariable Long id) {
        return R.ok(customerService.getCustomerById(id));
    }

    /**
     * 创建客户
     */
    @PostMapping
    @Operation(summary = "创建客户", description = "创建新客户")
    public R<CustomerDTO> createCustomer(@RequestBody CustomerDTO customerDTO) {
        return R.ok(customerService.createCustomer(customerDTO));
    }

    /**
     * 更新客户
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新客户", description = "更新客户信息")
    public R<CustomerDTO> updateCustomer(@PathVariable Long id, @RequestBody CustomerDTO customerDTO) {
        return R.ok(customerService.updateCustomer(id, customerDTO));
    }

    /**
     * 删除客户
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除客户", description = "删除客户")
    public R<Boolean> deleteCustomer(@PathVariable Long id) {
        return R.ok(customerService.deleteCustomer(id));
    }

    /**
     * 批量删除客户
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除客户", description = "批量删除客户")
    public R<Boolean> batchDeleteCustomers(@RequestBody List<Long> ids) {
        return R.ok(customerService.batchDeleteCustomers(ids));
    }

    /**
     * 更新客户状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新客户状态", description = "更新客户状态")
    public R<Boolean> updateCustomerStatus(@PathVariable Long id, @RequestParam String status) {
        return R.ok(customerService.updateCustomerStatus(id, status));
    }

    /**
     * 分配客户给业务员
     */
    @PutMapping("/{id}/assign")
    @Operation(summary = "分配客户", description = "分配客户给业务员")
    public R<Boolean> assignCustomer(@PathVariable Long id, @RequestParam Long userId) {
        return R.ok(customerService.assignCustomer(id, userId));
    }

    /**
     * 批量分配客户
     */
    @PutMapping("/batch-assign")
    @Operation(summary = "批量分配客户", description = "批量分配客户给业务员")
    public R<Boolean> batchAssignCustomers(@RequestBody List<Long> customerIds, @RequestParam Long userId) {
        return R.ok(customerService.batchAssignCustomers(customerIds, userId));
    }

    /**
     * 获取客户跟进记录
     */
    @GetMapping("/{id}/follow-ups")
    @Operation(summary = "获取客户跟进记录", description = "获取客户的跟进记录")
    public R<List<Object>> getCustomerFollowUps(@PathVariable Long id) {
        return R.ok(customerService.getCustomerFollowUps(id));
    }

    /**
     * 添加客户跟进记录
     */
    @PostMapping("/{id}/follow-ups")
    @Operation(summary = "添加客户跟进记录", description = "添加客户跟进记录")
    public R<Boolean> addCustomerFollowUp(@PathVariable Long id, @RequestBody Object followUpDTO) {
        return R.ok(customerService.addCustomerFollowUp(id, followUpDTO));
    }

    /**
     * 获取客户统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取客户统计信息", description = "获取客户统计信息")
    public R<Object> getCustomerStatistics() {
        return R.ok(customerService.getCustomerStatistics());
    }

    /**
     * 导出客户数据
     */
    @GetMapping("/export")
    @Operation(summary = "导出客户数据", description = "导出客户数据")
    public R<String> exportCustomers(@Parameter(description = "查询条件") CustomerQueryDTO queryDTO) {
        return R.ok(customerService.exportCustomers(queryDTO));
    }
}